package com.hugesoft.logic.managers.policemanager;

import java.util.Date;
import java.util.List;

import android.database.sqlite.SQLiteConstraintException;
import android.os.Handler;

import com.hugesoft.commonlogic.dao.DbManager;
import com.hugesoft.logic.managers.base.BaseBusinessLogicManager;
import com.hugesoft.logic.managers.common.Constans;
import com.hugesoft.logic.module.Police;
import com.hugesoft.logic.module.PoliceDao;

import de.greenrobot.dao.query.QueryBuilder;

public class PoliceManager extends BaseBusinessLogicManager implements
		IPoliceManagerInterface {
	private Police mCurrentPolice = null;

	@Override
	public void reg(String name, String policeno, String pwd, Handler handler) {
		Police police = new Police(null, name, policeno, pwd, new Date());
		try {
			DbManager.getPoliceDao().insert(police);
			handleregresponse(true, handler,null);
		} 
		catch (SQLiteConstraintException e)
		{
			handleregresponse(false, handler, "已经存在该警察了，请检查警号");
		}
		catch (Exception e) {
			handleregresponse(false, handler,"注册失败");
		}
	}

	@Override
	public void login(String policeno, String pwd, Handler handler) {
		QueryBuilder<Police> qBuilder = DbManager.getPoliceDao().queryBuilder();
		qBuilder.where(PoliceDao.Properties.Policeno.eq(policeno),
				PoliceDao.Properties.Password.eq(pwd));
		qBuilder.orderAsc(PoliceDao.Properties.Id);

		List<Police> policelist = qBuilder.list();
		if (policelist == null || policelist.size() == 0) {
			handleloginresponse(false, handler);
		} else {
			mCurrentPolice = policelist.get(0);
			Police p = new Police(mCurrentPolice.getId(), mCurrentPolice.getName(), mCurrentPolice.getPoliceno(), mCurrentPolice.getPassword(), new Date());
			DbManager.getPoliceDao().insertOrReplace(p);
			handleloginresponse(true, handler);
			
		}
	}

	private void handleregresponse(boolean isSuccess, Handler handler,String msg) {
		handleresponse(isSuccess ? Constans.REG_SUCCESS : Constans.REG_FAILED,
				msg, handler);
	}

	private void handleloginresponse(boolean isSuccess, Handler handler) {
		handleresponse(isSuccess ? Constans.LOGIN_SUCCESS
				: Constans.LOGIN_FAILED, null, handler);
	}

	public Police getcurrentpolice() {
		return mCurrentPolice;
	}

	@Override
	public List<Police> loadallpolice() {
		QueryBuilder<Police> qBuilder = DbManager.getPoliceDao().queryBuilder();
		qBuilder.orderAsc(PoliceDao.Properties.Id);

		List<Police> policelist = qBuilder.list();
		if (policelist == null || policelist.size() == 0) {
			return null;
		} else {
			return policelist;
		}
	}

	@Override
	public Police loadpolice(String policeno) {
		QueryBuilder<Police> qBuilder = DbManager.getPoliceDao().queryBuilder();
		qBuilder.where(PoliceDao.Properties.Policeno.eq(policeno));
		qBuilder.orderAsc(PoliceDao.Properties.Id);

		List<Police> policelist = qBuilder.list();
		if (policelist == null || policelist.size() == 0) {
			return null;
		} else {
			return policelist.get(0);
		}
	}

	@Override
	public Police loadpolice(Long policeid)
	{
		QueryBuilder<Police> qBuilder = DbManager.getPoliceDao().queryBuilder();
		qBuilder.where(PoliceDao.Properties.Id.eq(policeid));
		qBuilder.orderAsc(PoliceDao.Properties.Id);

		List<Police> policelist = qBuilder.list();
		if (policelist == null || policelist.size() == 0) {
			return null;
		} else {
			return policelist.get(0);
		}
	}
}
